package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.AccountDao;
import cn.sumpu.app.guoguojie.domain.Account;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao {
    private final static Logger logger = LoggerFactory.getLogger(AccountDaoImpl.class);

    @Override
    public void insertAccount(Account account) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.AccountMapper.insert", account);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Insert database failure.", e);
            }
            throw new AppBusinessException("Insert database failure.", e);
        }

    }

    @Override
    public void updateAccount(Account account) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.AccountMapper.update", account);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateAccount database failure.", e);
            }
            throw new AppBusinessException("updateAccount database failure.", e);
        }

    }

    @Override
    public void delAccountById(String id) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.AccountMapper.deleteAccountById", id);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Delete resource failure.", e);
            }
            throw new AppBusinessException("Delete resource failure.", e);
        }

    }

    @Override
    public Account getAccountById(String id) throws AppBusinessException {
        try {
            return (Account) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AccountMapper.getAccountById", id);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @Override
    public Account getAccountByName(String id) throws AppBusinessException {
        try {
            return (Account) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AccountMapper.getAccountByName", id);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query resource failure.", e);
            }
            throw new AppBusinessException("query resource failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Account> getAccountList(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Account>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.AccountMapper.getAccountList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @Override
    public Account getAccountByMember(String member) throws AppBusinessException {
        try {
            return (Account) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AccountMapper.getAccountByMember", member);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

}
